//
//  BKPhonePayView.m
//  bestkeep
//
//  Created by 李明 on 2016/12/13.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKPhonePayView.h"
#import "BKMainCommon.h"
#import "BKPhonePayViewCell.h"

@interface BKPhonePayView()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UIButton *payButton;
@property (nonatomic, strong) UITableView *payTableView;
@property (nonatomic, strong) OrderAlipayInfo *orderAlipayInfo;
@property (nonatomic, assign) BOOL isOpen;
@property (nonatomic, strong) NSString *nowPayWay;
@end
@implementation BKPhonePayView

static NSString *payViewCellIdentifier = @"payViewCellIdentifier";

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _isOpen = NO;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        CGFloat iphone = 0;
        if (iPhone4s) {
            iphone = 60;
        } else if (iPhone5) {
            iphone = 30;
        }
        self.payTableView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight / 3.0f * 2.0f + iphone);
        [UIView animateWithDuration:0.3 animations:^{
            self.payTableView.frame = CGRectMake(0, ScreenHeight / 3.0f - iphone, ScreenWidth, ScreenHeight / 3.0f * 2.0f + iphone);
        }];
        self.payButton.frame = CGRectMake(0, 0, ScreenWidth - 32, (ScreenWidth - 32) / 686.0f * 86.0f);
        self.payButton.center =  CGPointMake(ScreenWidth/2.0f, self.payTableView.bounds.size.height- 21 - (ScreenWidth - 32) /686.0f * 86.0f / 2.0f);
    }
    return self;
}

- (UITableView *)payTableView {
    if (!_payTableView) {
        
        _payTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        //[_payTableView setBackgroundColor:RGB(242, 242, 242)];
        [_payTableView setBackgroundColor:[UIColor whiteColor]];
        _payTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _payTableView.bounces = NO;
        [_payTableView registerClass:[BKPhonePayViewCell class] forCellReuseIdentifier:payViewCellIdentifier];
        [self addSubview:_payTableView];
    }
    return _payTableView;
}

- (UIButton *)payButton {
    if (!_payButton) {
        _payButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_payButton setTitle:@"确认付款" forState:UIControlStateNormal];
        _payButton.backgroundColor = RGB(0, 186, 156);
        _payButton.titleLabel.textColor = RGB(253, 253, 253);
        _payButton.titleLabel.font = UIFontLargest;
        _payButton.layer.cornerRadius = 4.0;
        [_payButton addTarget:self action:@selector(payButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.payTableView addSubview:_payButton];
    }
    return _payButton;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BKPhonePayViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:payViewCellIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:256 green:256 blue:256 alpha:1.0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell getPhonePayOrderAlipayInfo:self.orderAlipayInfo indexPath:indexPath pay_way_info:self.nowPayWay isOpen:self.isOpen];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 48;
    } else if (indexPath.row == 1)  {
        return 54;
    } else if (indexPath.row == 2) {
        return 54;
    } else if (indexPath.row == 3) {
        if (!_isOpen) {
            return 0.0001;
        }
        return 54;
    } else if (indexPath.row == 4) {
        if (!_isOpen) {
            return 0.0001;
        }
        return 54;
    } else if (indexPath.row == 5) {
        return 50;
    }
    return 0.001;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        [self cancelPage];
    } else if (indexPath.row == 2) {
        _isOpen = !_isOpen;
        [self startAnimation];
    } else if (indexPath.row == 3) {
        _isOpen = !_isOpen;
        self.payButton.tag = 0;
        self.nowPayWay = self.orderAlipayInfo.pay_way_info[0][@"pay_way"];
        [self startAnimation];
    } else if (indexPath.row == 4) {
        _isOpen = !_isOpen;
        self.payButton.tag = 1;
        self.nowPayWay = self.orderAlipayInfo.pay_way_info[1][@"pay_way"];
        [self startAnimation];
    }
}

// 点击CELL折叠动画
- (void)startAnimation {
    NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:2 inSection:0];
        NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:3 inSection:0];
        NSIndexPath *indexPath4 = [NSIndexPath indexPathForRow:4 inSection:0];
    [self.payTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath3, indexPath4, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.payTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath2, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)getOrderAlipayInfo:(OrderAlipayInfo *)orderAlipayInfo {
    self.orderAlipayInfo = orderAlipayInfo;
    self.nowPayWay = self.orderAlipayInfo.pay_way_info[0][@"pay_way"];
    self.payTableView.delegate = self;
    self.payTableView.dataSource = self;
}

- (void)cancelPage {
    if ([self.delegate respondsToSelector:@selector(clickCancelButton)]) {
        [self.delegate clickCancelButton];
    }
}

- (void)payButtonClick:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(clickPayButton:)]) {
        [self.delegate clickPayButton:sender];
    }
}

@end
